package com.sizu.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.basicdto.TCustomerDto;
import com.sizu.common.basicdto.TSupplierDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TCustomer;
import com.sizu.entity.TSupplier;
import com.sizu.service.ITCustomerService;
import com.sizu.service.ITSupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/basic/tCustomer")
public class TCustomerController {


    @Resource
    private ITCustomerService itCustomerService;

    /**
     * 查询客户列表
     * @param tCustomerDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tCustomerList(TCustomerDto tCustomerDto){
        PageHelper.startPage(tCustomerDto.getPageNum(),tCustomerDto.getPageSize());
        Wrapper<TCustomer> wrapper=new EntityWrapper<>();
        wrapper.eq("enable",1);
        if(null!=tCustomerDto.getName()&&!"".equals(tCustomerDto.getName())){
            wrapper.like("name",tCustomerDto.getName());
        }
        List<TCustomer> tCustomers = itCustomerService.selectList(wrapper);
        PageInfo<TCustomer> pageInfo=new PageInfo<>(tCustomers);
        return ResultEntity.ok("查询成功",pageInfo);
    }

    /**
     * 添加客户
     * @param tCustomerDto
     * @return
     */
    @RequestMapping("/add")
    public ResultEntity tCustomerAdd(TCustomerDto tCustomerDto){
        TCustomer tCustomer=new TCustomer();
        BeanUtils.copyProperties(tCustomerDto,tCustomer);
        itCustomerService.insert(tCustomer);
        return ResultEntity.ok("添加成功");
    }

    /**
     * 修改客户信息
     * @param tCustomerDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tCustomerUp(TCustomerDto tCustomerDto){
        TCustomer tCustomer=new TCustomer();
        BeanUtils.copyProperties(tCustomerDto,tCustomer);
        itCustomerService.updateById(tCustomer);
        return ResultEntity.ok("修改成功");
    }

    /**
     * 删除方法带批删
     * @param tCustomerDto
     * @return
     */
    @RequestMapping("/del")
    public ResultEntity tCustomerDel(TCustomerDto tCustomerDto){
        for (Integer id : tCustomerDto.getIds()) {
            TCustomer tCustomer=new TCustomer();
            tCustomer.setId(id);
            tCustomer.setEnable(0);
            itCustomerService.updateById(tCustomer);
        }
        return ResultEntity.ok("删除成功");
    }
}
